﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP.DataClass;
using System.Data.SqlClient;
using SMALLERP;
using XJLSC.PM;
using System.IO;

namespace XJLSC.QM
{
    public partial class QMSReport : Form
    {
        private readonly DataBase db = new DataBase();
        private SqlDataReader sdr1;

        public QMSReport()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strSql = "";
            try
            {
                strSql = "";
                if (radioButton1.Checked)
                {
                    strSql = "SELECT row_number() over (order by CD004) 序号,CD004 品号,MB002 品名,MB003 规格,INVMB.UDF01 老编码,0 AS 合格笔数,0.00 AS 合格数量, 0 AS 允收笔数,0.00 AS 允收数量, 0 AS 拒收笔数,0.00 AS 拒收数量, 0 AS 特采笔数,0.00 AS 特采数量, 0 AS 选别笔数,0.00 AS 选别数量, 0 AS 返工笔数,0.00 AS 返工数量 FROM QMSTA A LEFT JOIN PURCD ON TA001=CD001 AND TA002=CD002 AND TA003=CD003 LEFT JOIN INVMB ON CD004=MB001 WHERE TA001 LIKE '3%' AND TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND CD004 IS NOT NULL AND (CD004 LIKE '" + textBox1.Text + "%' OR '" + textBox1.Text + "'='') AND (INVMB.UDF01 LIKE '%" + textBox2.Text + "%' OR '" + textBox2.Text + "'='')  GROUP BY CD004,MB002,MB003,INVMB.UDF01";
                }

                if (radioButton2.Checked)
                {
                    strSql = "SELECT row_number() over (order by B.TA006) 序号,B.TA006 品号,MB002 品名,MB003 规格,INVMB.UDF01 老编码,0 AS 合格笔数,0.00 AS 合格数量, 0 AS 允收笔数,0.00 AS 允收数量, 0 AS 拒收笔数,0.00 AS 拒收数量, 0 AS 特采笔数,0.00 AS 特采数量, 0 AS 选别笔数,0.00 AS 选别数量, 0 AS 返工笔数,0.00 AS 返工数量 FROM QMSTA A LEFT JOIN MOCTA B ON A.TA001=B.TA001 AND A.TA002=B.TA002 LEFT JOIN INVMB ON B.TA006=MB001 WHERE (A.TA001='5110' OR A.TA001='5210' OR A.TA001='5160') AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TA006 IS NOT NULL AND (B.TA006 LIKE '" + textBox1.Text + "%' OR '" + textBox1.Text + "'='') AND (INVMB.UDF01 LIKE '%" + textBox2.Text + "%' OR '" + textBox2.Text + "'='') GROUP BY B.TA006,MB002,MB003,INVMB.UDF01";
                }

                if (radioButton3.Checked)
                {
                    strSql = "SELECT row_number() over (order by B.TO007) 序号,B.TO007 品号,MB002 品名,MB003 规格,INVMB.UDF01 老编码,0 AS 合格笔数,0.00 AS 合格数量, 0 AS 允收笔数,0.00 AS 允收数量, 0 AS 拒收笔数,0.00 AS 拒收数量, 0 AS 特采笔数,0.00 AS 特采数量, 0 AS 选别笔数,0.00 AS 选别数量, 0 AS 返工笔数,0.00 AS 返工数量 FROM QMSTA A LEFT JOIN COPTO B ON A.TA001=B.TO001 AND A.TA002=B.TO002 LEFT JOIN INVMB ON B.TO007=MB001 WHERE A.TA001 LIKE '2%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TO007 IS NOT NULL AND (B.TO007 LIKE '" + textBox1.Text + "%' OR '" + textBox1.Text + "'='') AND (INVMB.UDF01 LIKE '%" + textBox2.Text + "%' OR '" + textBox2.Text + "'='')   GROUP BY B.TO007,MB002,MB003,INVMB.UDF01";
                }

                if (radioButton4.Checked)
                {
                    strSql = "SELECT row_number() over (order by B.TG004) 序号,B.TG004 品号,MB002 品名,MB003 规格,INVMB.UDF01 老编码,0 AS 合格笔数,0.00 AS 合格数量, 0 AS 允收笔数,0.00 AS 允收数量, 0 AS 拒收笔数,0.00 AS 拒收数量, 0 AS 特采笔数,0.00 AS 特采数量, 0 AS 选别笔数,0.00 AS 选别数量, 0 AS 返工笔数,0.00 AS 返工数量 FROM QMSTA A LEFT JOIN MOCTG B ON A.TA001=B.TG001 AND A.TA002=B.TG002 AND A.TA003=B.TG003 LEFT JOIN INVMB ON B.TG004=MB001 WHERE A.TA001 LIKE '58%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TG004 IS NOT NULL AND (B.TG004 LIKE '" + textBox1.Text + "%' OR '" + textBox1.Text + "'='') AND (INVMB.UDF01 LIKE '%" + textBox2.Text + "%' OR '" + textBox2.Text + "'='') GROUP BY B.TG004,MB002,MB003,INVMB.UDF01";
                }

                if (radioButton5.Checked)
                {
                    strSql = "declare @sql1 VARCHAR(8000) set @sql1 = 'SELECT DISTINCT A.UDF02 AS 责任部门,A.UDF01 AS 供应商' select @sql1 = @sql1 + ' , SUM(case TL010 when ''' + TY003 + ''' then CEILING(A.TL006) else 0 end) [' + TY003 + ']' from (SELECT DISTINCT RQ001 AS TY003 FROM CMSRQ WHERE RQ001>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND RQ001<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112)) as a set @sql1 = @sql1 + ' FROM V_SCGY A group by A.UDF02,A.UDF01' EXEC(@sql1)";
                }


                System.Data.DataTable table = new DataTable();
                DataTable dt = this.dataGridView1.DataSource as DataTable;
                dataGridView1.DataSource = "";
                dataGridView1.Refresh();
                dt = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];

                dataGridView1.DataSource = dt;
                dt.Rows.Add();
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
                this.dataGridView1.AllowUserToAddRows = false;

                progressBar1.Visible = true;
                progressBar1.Maximum = dataGridView1.Rows.Count;//设置最大长度值
                progressBar1.Value = 0;//设置当前值
                progressBar1.Step = 1;//设置没次增长多少
                foreach (DataGridViewRow dr in this.dataGridView1.Rows)
                {
                    progressBar1.Value += progressBar1.Step;
                    if (radioButton1.Checked)
                    {
                        strSql = "SELECT COUNT(*) AS 合格笔数,SUM(CD008) AS 合格数量 FROM QMSTA A LEFT JOIN PURCD ON TA001=CD001 AND TA002=CD002 AND TA003=CD003 LEFT JOIN INVMB ON CD004=MB001 WHERE TA001 LIKE '3%' AND TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND CD004='" + dr.Cells["品号"].Value + "' AND A.UDF01='合格' AND CD004 IS NOT NULL GROUP BY CD004,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["合格笔数"].Index].Value = sdr1["合格笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["合格数量"].Index].Value = Convert.ToDouble(sdr1["合格数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        strSql = "SELECT COUNT(*) AS 允收笔数,SUM(CD008) AS 允收数量 FROM QMSTA A LEFT JOIN PURCD ON TA001=CD001 AND TA002=CD002 AND TA003=CD003 LEFT JOIN INVMB ON CD004=MB001 WHERE TA001 LIKE '3%' AND TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND CD004='" + dr.Cells["品号"].Value + "' AND A.UDF01='允收' AND CD004 IS NOT NULL GROUP BY CD004,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["允收笔数"].Index].Value = sdr1["允收笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["允收数量"].Index].Value = Convert.ToDouble(sdr1["允收数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        strSql = "SELECT COUNT(*) AS 拒收笔数,SUM(CD008) AS 拒收数量 FROM QMSTA A LEFT JOIN PURCD ON TA001=CD001 AND TA002=CD002 AND TA003=CD003 LEFT JOIN INVMB ON CD004=MB001 WHERE TA001 LIKE '3%' AND TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND CD004='" + dr.Cells["品号"].Value + "' AND A.UDF01='拒收' AND CD004 IS NOT NULL GROUP BY CD004,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["拒收笔数"].Index].Value = sdr1["拒收笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["拒收数量"].Index].Value = Convert.ToDouble(sdr1["拒收数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        strSql = "SELECT COUNT(*) AS 特采笔数,SUM(CD008) AS 特采数量 FROM QMSTA A LEFT JOIN PURCD ON TA001=CD001 AND TA002=CD002 AND TA003=CD003 LEFT JOIN INVMB ON CD004=MB001 WHERE TA001 LIKE '3%' AND TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND CD004='" + dr.Cells["品号"].Value + "' AND A.UDF01='特采' AND CD004 IS NOT NULL GROUP BY CD004,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["特采笔数"].Index].Value = sdr1["特采笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["特采数量"].Index].Value = Convert.ToDouble(sdr1["特采数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        strSql = "SELECT COUNT(*) AS 选别笔数,SUM(CD008) AS 选别数量 FROM QMSTA A LEFT JOIN PURCD ON TA001=CD001 AND TA002=CD002 AND TA003=CD003 LEFT JOIN INVMB ON CD004=MB001 WHERE TA001 LIKE '3%' AND TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND CD004='" + dr.Cells["品号"].Value + "' AND A.UDF01='选别' AND CD004 IS NOT NULL GROUP BY CD004,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["选别笔数"].Index].Value = sdr1["选别笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["选别数量"].Index].Value = Convert.ToDouble(sdr1["选别数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }


                        strSql = "SELECT COUNT(*) AS 返工笔数,SUM(CD008) AS 返工数量 FROM QMSTA A LEFT JOIN PURCD ON TA001=CD001 AND TA002=CD002 AND TA003=CD003 LEFT JOIN INVMB ON CD004=MB001 WHERE TA001 LIKE '3%' AND TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND CD004='" + dr.Cells["品号"].Value + "' AND A.UDF01='返工' AND CD004 IS NOT NULL GROUP BY CD004,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["返工笔数"].Index].Value = sdr1["返工笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["返工数量"].Index].Value = Convert.ToDouble(sdr1["返工数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                    }

                    if (radioButton2.Checked)
                    {
                        strSql = "SELECT COUNT(*) AS 合格笔数,SUM(A.TAC01) AS 合格数量 FROM QMSTA A LEFT JOIN MOCTA B ON A.TA001=B.TA001 AND A.TA002=B.TA002 LEFT JOIN INVMB ON B.TA006=MB001 WHERE A.TA001 LIKE '5%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TA006='" + dr.Cells["品号"].Value + "' AND A.UDF01='合格' AND B.TA006 IS NOT NULL GROUP BY B.TA006,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["合格笔数"].Index].Value = sdr1["合格笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["合格数量"].Index].Value = Convert.ToDouble(sdr1["合格数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        strSql = "SELECT COUNT(*) AS 允收笔数,SUM(A.TAC01) AS 允收数量 FROM QMSTA A LEFT JOIN MOCTA B ON A.TA001=B.TA001 AND A.TA002=B.TA002 LEFT JOIN INVMB ON B.TA006=MB001 WHERE A.TA001 LIKE '5%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TA006='" + dr.Cells["品号"].Value + "' AND A.UDF01='允收' AND B.TA006 IS NOT NULL GROUP BY B.TA006,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["允收笔数"].Index].Value = sdr1["允收笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["允收数量"].Index].Value = Convert.ToDouble(sdr1["允收数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        strSql = "SELECT COUNT(*) AS 拒收笔数,SUM(A.TAC01) AS 拒收数量 FROM QMSTA A LEFT JOIN MOCTA B ON A.TA001=B.TA001 AND A.TA002=B.TA002 LEFT JOIN INVMB ON B.TA006=MB001 WHERE A.TA001 LIKE '5%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TA006='" + dr.Cells["品号"].Value + "' AND A.UDF01='拒收' AND B.TA006 IS NOT NULL GROUP BY B.TA006,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["拒收笔数"].Index].Value = sdr1["拒收笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["拒收数量"].Index].Value = Convert.ToDouble(sdr1["拒收数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        strSql = "SELECT COUNT(*) AS 特采笔数,SUM(A.TAC01) AS 特采数量 FROM QMSTA A LEFT JOIN MOCTA B ON A.TA001=B.TA001 AND A.TA002=B.TA002 LEFT JOIN INVMB ON B.TA006=MB001 WHERE A.TA001 LIKE '5%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TA006='" + dr.Cells["品号"].Value + "' AND A.UDF01='特采' AND B.TA006 IS NOT NULL GROUP BY B.TA006,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["特采笔数"].Index].Value = sdr1["特采笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["特采数量"].Index].Value = Convert.ToDouble(sdr1["特采数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        strSql = "SELECT COUNT(*) AS 选别笔数,SUM(A.TAC01) AS 选别数量 FROM QMSTA A LEFT JOIN MOCTA B ON A.TA001=B.TA001 AND A.TA002=B.TA002 LEFT JOIN INVMB ON B.TA006=MB001 WHERE A.TA001 LIKE '5%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TA006='" + dr.Cells["品号"].Value + "' AND A.UDF01='选别' AND B.TA006 IS NOT NULL GROUP BY B.TA006,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["选别笔数"].Index].Value = sdr1["选别笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["选别数量"].Index].Value = Convert.ToDouble(sdr1["选别数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }


                        strSql = "SELECT COUNT(*) AS 返工笔数,SUM(A.TAC01) AS 返工数量 FROM QMSTA A LEFT JOIN MOCTA B ON A.TA001=B.TA001 AND A.TA002=B.TA002 LEFT JOIN INVMB ON B.TA006=MB001 WHERE A.TA001 LIKE '5%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TA006='" + dr.Cells["品号"].Value + "' AND A.UDF01='返工' AND B.TA006 IS NOT NULL GROUP BY B.TA006,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["返工笔数"].Index].Value = sdr1["返工笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["返工数量"].Index].Value = Convert.ToDouble(sdr1["返工数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                    }

                    if (radioButton3.Checked)
                    {
                        strSql = "SELECT COUNT(*) AS 合格笔数,SUM(A.TAC01) AS 合格数量 FROM QMSTA A LEFT JOIN COPTO B ON A.TA001=B.TO001 AND A.TA002=B.TO002 AND A.TA003=B.TO003 LEFT JOIN INVMB ON B.TO007=MB001 WHERE A.TA001 LIKE '2%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TO007='" + dr.Cells["品号"].Value + "' AND A.UDF01='合格' AND B.TO007 IS NOT NULL GROUP BY B.TO007,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["合格笔数"].Index].Value = sdr1["合格笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["合格数量"].Index].Value = Convert.ToDouble(sdr1["合格数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        strSql = "SELECT COUNT(*) AS 允收笔数,SUM(A.TAC01) AS 允收数量 FROM QMSTA A LEFT JOIN COPTO B ON A.TA001=B.TO001 AND A.TA002=B.TO002 AND A.TA003=B.TO003 LEFT JOIN INVMB ON B.TO007=MB001 WHERE A.TA001 LIKE '2%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TO007='" + dr.Cells["品号"].Value + "' AND A.UDF01='允收' AND B.TO007 IS NOT NULL GROUP BY B.TO007,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["允收笔数"].Index].Value = sdr1["允收笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["允收数量"].Index].Value = Convert.ToDouble(sdr1["允收数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        strSql = "SELECT COUNT(*) AS 拒收笔数,SUM(A.TAC01) AS 拒收数量 FROM QMSTA A LEFT JOIN COPTO B ON A.TA001=B.TO001 AND A.TA002=B.TO002 AND A.TA003=B.TO003 LEFT JOIN INVMB ON B.TO007=MB001 WHERE A.TA001 LIKE '2%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TO007='" + dr.Cells["品号"].Value + "' AND A.UDF01='返工' AND B.TO007 IS NOT NULL GROUP BY B.TO007,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["拒收笔数"].Index].Value = sdr1["拒收笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["拒收数量"].Index].Value = Convert.ToDouble(sdr1["拒收数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        strSql = "SELECT COUNT(*) AS 特采笔数,SUM(A.TAC01) AS 特采数量 FROM QMSTA A LEFT JOIN COPTO B ON A.TA001=B.TO001 AND A.TA002=B.TO002 AND A.TA003=B.TO003 LEFT JOIN INVMB ON B.TO007=MB001 WHERE A.TA001 LIKE '2%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TO007='" + dr.Cells["品号"].Value + "' AND A.UDF01='特采' AND B.TO007 IS NOT NULL GROUP BY B.TO007,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["特采笔数"].Index].Value = sdr1["特采笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["特采数量"].Index].Value = Convert.ToDouble(sdr1["特采数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        strSql = "SELECT COUNT(*) AS 选别笔数,SUM(A.TAC01) AS 选别数量 FROM QMSTA A LEFT JOIN COPTO B ON A.TA001=B.TO001 AND A.TA002=B.TO002 AND A.TA003=B.TO003 LEFT JOIN INVMB ON B.TO007=MB001 WHERE A.TA001 LIKE '2%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TO007='" + dr.Cells["品号"].Value + "' AND A.UDF01='选别' AND B.TO007 IS NOT NULL GROUP BY B.TO007,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["选别笔数"].Index].Value = sdr1["选别笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["选别数量"].Index].Value = Convert.ToDouble(sdr1["选别数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }


                        strSql = "SELECT COUNT(*) AS 返工笔数,SUM(A.TAC01) AS 返工数量 FROM QMSTA A LEFT JOIN COPTO B ON A.TA001=B.TO001 AND A.TA002=B.TO002 AND A.TA003=B.TO003 LEFT JOIN INVMB ON B.TO007=MB001 WHERE A.TA001 LIKE '2%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TO007='" + dr.Cells["品号"].Value + "' AND A.UDF01='返工' AND B.TO007 IS NOT NULL GROUP BY B.TO007,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["返工笔数"].Index].Value = sdr1["返工笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["返工数量"].Index].Value = Convert.ToDouble(sdr1["返工数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                    }

                    if (radioButton4.Checked)
                    {
                        strSql = "SELECT COUNT(*) AS 合格笔数,SUM(A.TAC01) AS 合格数量 FROM QMSTA A LEFT JOIN MOCTG B ON A.TA001=B.TG001 AND A.TA002=B.TG002 AND A.TA003=B.TG003 LEFT JOIN INVMB ON B.TG004=MB001 WHERE A.TA001 LIKE '58%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TG004='" + dr.Cells["品号"].Value + "' AND A.UDF01='合格' AND B.TG004 IS NOT NULL GROUP BY B.TG004,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["合格笔数"].Index].Value = sdr1["合格笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["合格数量"].Index].Value = Convert.ToDouble(sdr1["合格数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        strSql = "SELECT COUNT(*) AS 允收笔数,SUM(A.TAC01) AS 允收数量 FROM QMSTA A LEFT JOIN MOCTG B ON A.TA001=B.TG001 AND A.TA002=B.TG002 AND A.TA003=B.TG003 LEFT JOIN INVMB ON B.TG004=MB001 WHERE A.TA001 LIKE '58%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TG004='" + dr.Cells["品号"].Value + "' AND A.UDF01='允收' AND B.TG004 IS NOT NULL GROUP BY B.TG004,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["允收笔数"].Index].Value = sdr1["允收笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["允收数量"].Index].Value = Convert.ToDouble(sdr1["允收数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        strSql = "SELECT COUNT(*) AS 拒收笔数,SUM(A.TAC01) AS 拒收数量 FROM QMSTA A LEFT JOIN MOCTG B ON A.TA001=B.TG001 AND A.TA002=B.TG002 AND A.TA003=B.TG003 LEFT JOIN INVMB ON B.TG004=MB001 WHERE A.TA001 LIKE '58%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TG004='" + dr.Cells["品号"].Value + "' AND A.UDF01='拒收' AND B.TG004 IS NOT NULL GROUP BY B.TG004,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["拒收笔数"].Index].Value = sdr1["拒收笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["拒收数量"].Index].Value = Convert.ToDouble(sdr1["拒收数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        strSql = "SELECT COUNT(*) AS 特采笔数,SUM(A.TAC01) AS 特采数量 FROM QMSTA A LEFT JOIN MOCTG B ON A.TA001=B.TG001 AND A.TA002=B.TG002 AND A.TA003=B.TG003 LEFT JOIN INVMB ON B.TG004=MB001 WHERE A.TA001 LIKE '58%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TG004='" + dr.Cells["品号"].Value + "' AND A.UDF01='特采' AND B.TG004 IS NOT NULL GROUP BY B.TG004,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["特采笔数"].Index].Value = sdr1["特采笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["特采数量"].Index].Value = Convert.ToDouble(sdr1["特采数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        strSql = "SELECT COUNT(*) AS 选别笔数,SUM(A.TAC01) AS 选别数量 FROM QMSTA A LEFT JOIN MOCTG B ON A.TA001=B.TG001 AND A.TA002=B.TG002 AND A.TA003=B.TG003 LEFT JOIN INVMB ON B.TG004=MB001 WHERE A.TA001 LIKE '58%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TG004='" + dr.Cells["品号"].Value + "' AND A.UDF01='选别' AND B.TG004 IS NOT NULL GROUP BY B.TG004,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["选别笔数"].Index].Value = sdr1["选别笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["选别数量"].Index].Value = Convert.ToDouble(sdr1["选别数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }


                        strSql = "SELECT COUNT(*) AS 返工笔数,SUM(A.TAC01) AS 返工数量 FROM QMSTA A LEFT JOIN MOCTG B ON A.TA001=B.TG001 AND A.TA002=B.TG002 AND A.TA003=B.TG003 LEFT JOIN INVMB ON B.TG004=MB001 WHERE A.TA001 LIKE '58%' AND A.TA004>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND A.TA004<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND B.TG004='" + dr.Cells["品号"].Value + "' AND A.UDF01='返工' AND B.TG004 IS NOT NULL GROUP BY B.TG004,MB002,MB003";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                dr.Cells[this.dataGridView1.Columns["返工笔数"].Index].Value = sdr1["返工笔数"].ToString();
                                dr.Cells[this.dataGridView1.Columns["返工数量"].Index].Value = Convert.ToDouble(sdr1["返工数量"]).ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }

                    }
                }

                progressBar1.Visible = false;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }
            if (radioButton5.Checked)
            {
                this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Cells[this.dataGridView1.Columns["供应商"].Index].Value = "合计";
                this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].DefaultCellStyle.BackColor = Color.Yellow;
                for (int i = this.dataGridView1.Columns["供应商"].Index + 1; i <= dataGridView1.Columns.Count - 1; i++)
                {
                    double sum2 = 0;
                    for (int l = 0; l < this.dataGridView1.Rows.Count - 1; l++)
                    {
                        sum2 += double.Parse(this.dataGridView1.Rows[l].Cells[i].Value == null ? "0" : this.dataGridView1.Rows[l].Cells[i].Value.ToString());
                    }
                    this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Cells[i].Value = sum2;
                }
            }
            else
            {
                this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Cells[this.dataGridView1.Columns[""].Index].Value = "合计";
                this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].DefaultCellStyle.BackColor = Color.Yellow;
                for (int i = this.dataGridView1.Columns["老编码"].Index + 1; i <= dataGridView1.Columns.Count - 1; i++)
                {
                    double sum2 = 0;
                    for (int l = 0; l < this.dataGridView1.Rows.Count - 1; l++)
                    {
                        sum2 += double.Parse(this.dataGridView1.Rows[l].Cells[i].Value == null ? "0" : this.dataGridView1.Rows[l].Cells[i].Value.ToString());
                    }
                    this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Cells[i].Value = sum2;
                }
            }

        }

        private void QMSReport_Load(object sender, EventArgs e)
        {
            progressBar1.Visible = false;
            radioButton5.Checked = true;
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string x1 = "";
            string x2 = "";
            string x3 = "";
            string x4 = "";
            string x5 = "";
            if (radioButton5.Checked)
            {
                x1 = dataGridView1["责任部门", dataGridView1.CurrentCell.RowIndex].Value.ToString().Trim() + dataGridView1["供应商", dataGridView1.CurrentCell.RowIndex].Value.ToString().Trim();
                x2 = this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name.ToString();
                x3 = dateTimePicker2.Value.ToShortDateString();
            }
            else
            {
                x1 = dataGridView1["品号", dataGridView1.CurrentCell.RowIndex].Value.ToString();
                x2 = dateTimePicker1.Value.ToShortDateString();
                x3 = dateTimePicker2.Value.ToShortDateString();
            }
            x4 = "查看检验结果";

            if (radioButton1.Checked)
            {
                x5 = "IQC";
            }

            if (radioButton2.Checked)
            {
                x5 = "FQC";
            }

            if (radioButton3.Checked)
            {
                x5 = "OQC";
            }

            if (radioButton4.Checked)
            {
                x5 = "PQC";
            }
            if (radioButton5.Checked)
            {
                x5 = "综合";
            }
            if (x4 != "其他")
            {
                QJCX xx = new QJCX(x1, x2, x3, x4, x5);
                QMSCHAXUN f = new QMSCHAXUN();
                f.cdgzzx += new QMSCHAXUN.Cdgzzx(textBox2_TextChanged);
                f.Show(this);
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CreatePrompt = true;
            dlg.Title = "保存为Excel文件";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Stream myStream;
                myStream = dlg.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                string columnTitle = null;
                try
                {
                    //写入列标题    
                    for (int i = 0; i < dataGridView1.ColumnCount; i++)
                    {
                        if (dataGridView1.Columns[i].Visible == true)
                        {
                            if (i > 0)
                            {
                                columnTitle += "\t";
                            }
                            columnTitle += dataGridView1.Columns[i].HeaderText;

                        }

                    }
                    sw.WriteLine(columnTitle);
                    //写入列内容    
                    for (int j = 0; j < dataGridView1.Rows.Count; j++)
                    {

                        string columnValue = null;
                        for (int k = 0; k < dataGridView1.Columns.Count; k++)
                        {
                            if (dataGridView1.Columns[k].Visible == true)
                            {
                                if (k > 0)
                                {
                                    columnValue += "\t";
                                }
                                if (dataGridView1.Rows[j].Cells[k].Value == null)
                                    columnValue += "";
                                else
                                    columnValue += dataGridView1.Rows[j].Cells[k].Value.ToString().Trim();
                            }
                        }
                        sw.WriteLine(columnValue);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            String strSql = "";
            strSql = "SELECT TE023 生产日期,ISNULL(TA027,'') 订单号,TE006 工单单别,TE007 工单单号,TE017 品号,TE018 品名,TE019 规格,RTRIM(ISNULL(SFCTE.UDF06,'')) 工序,RTRIM(ISNULL(SFCTE.UDF07,'')) 作业人员,CAST(SFCTE.UDF51 AS REAL) 料废,CAST(SFCTE.UDF52 AS REAL) 工废,CAST(SFCTE.UDF53 AS REAL) 不良 FROM SFCTE LEFT JOIN MOCTA ON TE006=TA001 AND TE007=TA002 WHERE SFCTE.UDF51+SFCTE.UDF52>0 AND TE023>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker3.Value.ToShortDateString() + "'),112) AND TE023<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker4.Value.ToShortDateString() + "'),112) AND TE001='D410'";
      
            try
            {
                dataGridView2.DataSource = "";
                dataGridView2.Refresh();
                dataGridView2.DataSource = db.GetDataSet1(strSql, "MOCTB").Tables["MOCTB"];
            }
          catch
             {
             }

        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CreatePrompt = true;
            dlg.Title = "保存为Excel文件";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Stream myStream;
                myStream = dlg.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                string columnTitle = null;
                try
                {
                    //写入列标题    
                    for (int i = 0; i < dataGridView2.ColumnCount; i++)
                    {
                        if (dataGridView2.Columns[i].Visible == true)
                        {
                            if (i > 0)
                            {
                                columnTitle += "\t";
                            }
                            columnTitle += dataGridView2.Columns[i].HeaderText;

                        }

                    }
                    sw.WriteLine(columnTitle);
                    //写入列内容    
                    for (int j = 0; j < dataGridView2.Rows.Count; j++)
                    {

                        string columnValue = null;
                        for (int k = 0; k < dataGridView2.Columns.Count; k++)
                        {
                            if (dataGridView2.Columns[k].Visible == true)
                            {
                                if (k > 0)
                                {
                                    columnValue += "\t";
                                }
                                if (dataGridView2.Rows[j].Cells[k].Value == null)
                                    columnValue += "";
                                else
                                    columnValue += dataGridView2.Rows[j].Cells[k].Value.ToString().Trim();
                            }
                        }
                        sw.WriteLine(columnValue);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }
        }
    }
}